/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.layers;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import id.duapuluhdua.dakidaki.MainState;
import id.duapuluhdua.dakidaki.Preferences;
import id.duapuluhdua.dakidaki.common.CommonActivity;

/**
 *
 */
public abstract class TipFrame
{
  /** Constants */
  protected final static String DUMMY_TEXT = "Text";
  protected final static int PADDING = 8;
  protected final static int LINE_HEIGHT_FOR_TITLES = 10;
  protected final static int LINE_HEIGHT = 6;

  /** Fields */
  protected CommonActivity mParent;
  protected Preferences mPreference;
  protected MainState mState;

  private Paint mShape;
  private Paint mShadow;
  private Paint mBorder;
  private Paint mTitle;

  private int mTop = 0;
  private int mLeft = 0;
  private int mRight = 0;

  private String mTitleString;

  protected RectF mRtShadow;  // the frame shadow
  protected RectF mRtShape;  // the frame panel
  protected Rect mRtTitle;  // the frame title

  /**
   * The constructor.
   *
   * @param parent
   *            The application preferences.
   * @param state
   *            The application state object.
   * @param title
   *            The title of the tip frame.
   */
  public TipFrame(CommonActivity parent, MainState state, String title)
  {
    mParent = parent;
    mPreference = parent.getPrefs();
    mState = state;
    mTitleString = title;

    // For drawing shapes
    mShape = new Paint();
    mShape.setAntiAlias(true);
    mShape.setColor(0xffffff88);
    mShape.setStrokeWidth(1);
    mShape.setStyle(Paint.Style.FILL);

    // For drawing shadows
    mShadow = new Paint();
    mShadow.setAntiAlias(true);
    mShadow.setColor(0x22888888);
    mShadow.setStrokeWidth(1);
    mShadow.setStyle(Paint.Style.FILL);

    // For drawing borders
    mBorder = new Paint();
    mBorder.setAntiAlias(true);
    mBorder.setColor(0xffdada00);
    mBorder.setStrokeWidth(2);
    mBorder.setStyle(Paint.Style.STROKE);

    // For drawing titles
    mTitle = new Paint();
    mTitle.setAntiAlias(true);
    mTitle.setColor(0xffaa5500);
    mTitle.setFakeBoldText(true);
    mTitle.setStyle(Paint.Style.FILL);
    mTitle.setTextSize(toPixel(14));
    mTitle.setTextAlign(Paint.Align.CENTER);

    mRtShadow = new RectF();
    mRtShape = new RectF();
    mRtTitle = new Rect(0, 0, 0, 0);
  }

  /**
   * Set the frame position measured from the top.
   *
   * @param top the distance from the top screen (in pixel).
   */
  public void setPosition(int top)
  {
    mTop = top;
  }

  /**
   * Set the frame left and right margin margin.
   *
   * @param left the distance from the left screen (in pixel).
   * @param right the distance from the right screen (in pixel).
   */
  public void setMargins(int left, int right)
  {
    mLeft = left;
    mRight = right;
  }

  /**
   * Get the height of the tip frame.
   *
   * @return the height size.
   */
  protected int getHeight()
  {
    return getTitleHeight();
  }

  /**
   * Get the lowest point of the title string.
   *
   * @return the lowest point.
   */
  protected int getTitleBottom()
  {
    return (int) mRtShape.top + PADDING + mRtTitle.height() + LINE_HEIGHT_FOR_TITLES;
  }

  /**
   * Draw the tip frame.
   *
   * @param canvas the canvas object.
   * @param area
   */
  public void doDraw(Canvas canvas, Rect area)
  {
    final int height = getHeight();

    mRtShape.set(area);

    mRtShape.top += mTop;
    mRtShape.left += mLeft;
    mRtShape.right -= mRight;
    mRtShape.bottom = mRtShape.top + height;

    // Draw shadow
    mRtShadow.set(mRtShape);
    mRtShadow.inset(-4, -4);
    canvas.drawRoundRect(mRtShadow, 6, 6, mShadow);

    // Draw background
    canvas.drawRoundRect(mRtShape, 4, 4, mShape);
    canvas.drawRoundRect(mRtShape, 4, 4, mBorder);

    // Draw title
    final int titlePosY = (int) mRtShape.top + PADDING + mRtTitle.height();
    canvas.drawText(mTitleString, mRtShape.centerX(), titlePosY, mTitle);
  }

  private int getTitleHeight()
  {
    mTitle.getTextBounds(DUMMY_TEXT, 0, DUMMY_TEXT.length(), mRtTitle);

    int value = PADDING;
    value += mRtTitle.height();
    value += LINE_HEIGHT_FOR_TITLES;

    return value;
  }

  /**
   * Converts the dps to pixels.
   *
   * @param dips
   * @return
   */
  protected float toPixel(float dips)
  {
    final float scale = mParent.getResources().getDisplayMetrics().density;
    return (dips * scale + 0.5f);
  }
}